<script>
	import {getOpenIDAction,LocationResolution,getMemberInfoAction,getAddAction} from '@/api/global.js';
	import {getCurrentTimestamp} from '@/static/js/DateUtil.js'
	export default {
		data() {
			return {
				memberInfo: {
					//用户表
					Member_SerialNumber: "",
					Nick: "游客",
					Image: "/static/icon/tourist.png",
					Member_Name: "",
					Mobile: "",
					Head_URL: "",
					Open_ID: "",
					Sales_ID: "",
					Login_authorization: 0,
					Rights_Type: 0,
					Dealer_Authority: "",
					Employee_Authority: 0,
					App_ID: uni.getAccountInfoSync().miniProgram.appId,
					Appoint_Member_Type: 0,
					Enterprise_Info_SerialNumber: "E00000000000001",
					Children: {
						member_account_info: [{
							Member_Account_SerialNumber: "",
							Member_SerialNumber: "",
							Account_Type: 0,
							Prepayment: 0,
							Prepayment_Sales: 0,
							Prepayment_Balance: 0,
							Gift_Amount: 0,
							Gift_Sales: 0,
							Gift_Balance: 0,
							Sales_Point: 0,
							Sales_Point_Sales: 0,
							Sales_Point_Balance: 0,
							Gift_Point: 0,
							Gift_Point_Sales: 0,
							Gift_Point_Balance: 0,
						}],
					},
				},
				position: {},
			};
		},
		globalData: { //全局变量
			Domain_IMG: "https://digital.essc.top/dist/imgxklj", //图片域名
			// Domain_IMG: "https://3451m7y168.goho.co/svxklj/img", 
			// Domain_IMG: '/static/img',
			Domain_ICON: "https://digital.essc.top/dist/iconxklj", //图标
			Domain_dead:"/static/icon",   // 测试
			Open_ID: "ojEAP4-rf7FJNdyMzEN5xcpXmjSk", //用户Open_ID
			// App_ID: "wx9b74a42a123612c9",
			// App_ID: "wxd96a1f0fbc125263",
			App_ID: uni.getAccountInfoSync().miniProgram.appId, //小程序appid
			Enterprise_Info_SerialNumber: "E00000000000001", //企业编号
			memberInfo: {}, //用户信息
		},
		onLaunch: function() {
			uni.showLoading({title: "加载中..."});
			let ls_App_ID = this.$scope.globalData.App_ID;
			let ls_Enterprise_Info_SerialNumber = this.$scope.globalData.Enterprise_Info_SerialNumber
			this.getMyLocation((location) => {
				//获取Open_ID
				this.onLogin((loginRes) => {
					console.log(1);
					this.getOpenID(loginRes.code, ls_App_ID, (Res) => {
						console.log(2);
						uni.setStorageSync("ShareUrl", `/pages/index/index?openid=${Res.Open_ID}`);
						this.$scope.globalData.Open_ID = Res.Open_ID
						//获取用户信息
						this.getMemberInfo(Res.Open_ID, ls_App_ID, ls_Enterprise_Info_SerialNumber, ({Data}) => {
							console.log(3);
							uni.hideLoading();
							uni.reLaunch({url:'/pages/index/index'})
							// 判断是否有用户信息，无则提醒注册，有则直接保存
							if (Data.length>0) {//存储当前用户信息
								this.$scope.globalData.memberInfo = Data[0];
							} else {
								// 添加默认用户信息
								this.memberInfo.Open_ID = Res.Open_ID;
								this.getAddAction({Data:JSON.stringify({member_info:[this.memberInfo]}),Open_ID:Res.Open_ID},({Status,Data})=>{
									if(Status=="success"){
										this.$scope.globalData.memberInfo = this.memberInfo
										uni.showModal({title:"提示",content:"是否完善个人信息",success:(res)=>{
											if(res.confirm){// 成功跳转修改信息页
												uni.navigateTo({url:'/pages/my/information?Member_SerialNumber='+Data})
											}
										}})
									}
								})
							}
						})
					})
				})
			});
		},
		methods: {
			onLogin(callback) { //获取用户登录Code码
				uni.login({
					provider: "weixin",
					success: function(loginRes) {
						callback(loginRes)
					}
				})
			},
			async getOpenID(Code, App_ID, callback) { //获取用户Open_ID
				callback(await getOpenIDAction({Code,App_ID}))
			},
			async getMemberInfo(Open_ID, App_ID, Enterprise_Info_SerialNumber, callback) { //查询用户信息
				callback(await getMemberInfoAction({Open_ID,App_ID,Enterprise_Info_SerialNumber}))
			},
			getMyLocation(callback) { //获取缓存中用户地址
				let AddressLocation = uni.getStorageSync("AddressLocation")
				if (AddressLocation && AddressLocation.expires_in > getCurrentTimestamp()) {
					callback(AddressLocation.data)
				} else {
					this.getLocation((data) => {
						uni.setStorageSync("AddressLocation", {data,expires_in: getCurrentTimestamp() + 1800})
						callback(data)
					})
				}
			},
			getLocation(callback) { //获取用户当前地址
				uni.getLocation({
					type: 'gcj02',
					success: async ({latitude,longitude}) => {
						const res = await LocationResolution({latitude,longitude,Open_ID: this.$scope.globalData.Open_ID})
						callback({
							longitude: res.Data.location.lng,
							latitude: res.Data.location.lat,
							province: res.Data.address_component.province,
							city: res.Data.address_component.city,
							district: res.Data.address_component.district,
							street: res.Data.address_component.street,
							address: res.Data.address,
						})
					}
				})
			},
			async getAddAction(Data,callback){
				callback(await getAddAction(Data));
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@font-face {
		font-family: iconfont;
		src: url('static/css/iconfont.ttf');
	}

	@import "static/css/free.css";
</style>